


import 'package:moonplay/pages/common_page/common_text_page.dart';
import 'package:flutter/material.dart';
import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/model/search_video_model.dart';
import 'package:get/get.dart';

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  TextEditingController _textController = TextEditingController();
  String searchText;

  SearchVideoModel searchVideoModel;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xffffffff),
      appBar: CustomAppbar(
        navigationBarBackgroundColor: Theme.of(context).primaryColorLight,
        leadingWidget: Container(),
        title: Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Expanded(
                child: Container(
                  height: ScreenUtil().getWidthPx(66),
                  color: Colors.transparent,
                  margin: EdgeInsets.only(left: ScreenUtil().getWidthPx(24)),
                  child: TextField(
                    style: TextStyle(fontSize: ScreenUtil().getWidthPx(28), color: Colors.black),//输入文本的样式
                    decoration: InputDecoration(
                      contentPadding: EdgeInsets.symmetric(vertical: 4.0),
                      hintText: '请输入影视名称',
                      hintStyle: TextStyle(fontSize: ScreenUtil().getWidthPx(28), color: Color(0xffb6b6b6)),
                      prefixIcon: Icon(Icons.search, color: Color(0xffb6b6b6),),
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(4),
                          borderSide: BorderSide.none),
                      filled: true,
                      fillColor: Color(0xffffffff),
                    ),
                    autofocus: true,
                    textInputAction: TextInputAction.search,
                    onSubmitted: (text) {
                      searchFn();
                    },
                    onChanged: (val){
                      setState(() {
                        searchText = val;
                      });
                    },
                  ),
                ),
              ),
              InkResponse(
                highlightColor: Colors.transparent,
                radius: 0.0,
                onTap: (){
                  print('点击取消');
                  Get.back();
                },
                child: Container(
                  width: ScreenUtil().getWidthPx(106),
                  height: ScreenUtil().getWidthPx(CommonHeight.appBarHeight),
                  alignment: Alignment.center,
                  child: Cmtext('取消', style: TextStyle(color: Colors.white),),
                ),
              )
            ],
          ),
        )
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: ListView(
          children: searchVideoModel != null ? _widgetList() : [],
        ),
      )
    );
  }

  List<Widget> _widgetList(){
    List<Widget> _list = [];
    for (var i = 0; i < searchVideoModel.records.length; i++) {
      Widget _addWidget = Container(
        height: ScreenUtil().getWidthPx(360),
        padding: EdgeInsets.symmetric(vertical: ScreenUtil().getWidthPx(30), horizontal: ScreenUtil().getWidthPx(30)),
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: Container(
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(0),
                  child: loadNetworkImage(searchVideoModel.records[i].vodPic),
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: Container(
                margin: EdgeInsets.only(left: ScreenUtil().getWidthPx(20)),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Expanded(
                      flex: 4,
                      child: Container(
                        child: Column(
                          children: <Widget>[
                            Container(
                              height: ScreenUtil().getWidthPx(50),
                              alignment: Alignment.centerLeft,
                              child: Cmtext('${searchVideoModel.records[i].vodName}', style: TextStyle(color: Color(0xff009dff)),),
                            ),
                            Container(
                              width: 300,
                              height: ScreenUtil().getWidthPx(50),
                              alignment: Alignment.centerLeft,
                              child: Row(
                                children: [
                                  Cmtext('${searchVideoModel.records[i].vodYear}/${searchVideoModel.records[i].vodArea}/', style: TextStyle(color: Color(0xff969696), fontSize: 13),),
                                  typeText('${searchVideoModel.records[i].typeId1}', '${searchVideoModel.records[i].typeId}')
                                ],
                              )
                            ),
                            Container(
                              height: ScreenUtil().getWidthPx(50),
                              alignment: Alignment.centerLeft,
                              child: Cmtext('${searchVideoModel.records[i].vodDirector}', style: TextStyle(color: Color(0xff969696), fontSize: 13),),
                            ),
                            Container(
                              constraints: BoxConstraints(
                                minHeight: ScreenUtil().getWidthPx(50),
                              ),
                              alignment: Alignment.centerLeft,
                              child: Cmtext('${searchVideoModel.records[i].vodActor}', style: TextStyle(color: Color(0xff969696), fontSize: 13),overflow: TextOverflow.ellipsis,maxLines: 2,),
                            ),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: ScreenUtil().getWidthPx(100),
                        child: Row(
                          children: <Widget>[
                            GestureDetector(
                              onTap: (){
                                FocusScope.of(context).requestFocus(FocusNode());
                                int typeId1;
                                int typeId1_tt = searchVideoModel.records[i].typeId1;
                                int typeId = searchVideoModel.records[i].typeId;
                                if (typeId1_tt == 0) {
                                  if (typeId == 3) {
                                    typeId1 = 3;
                                  } else {
                                    typeId1 = 4;
                                  }
                                } else {
                                  typeId1 = typeId1_tt;
                                }
                                Get.toNamed('/VideoDetailPage', arguments: {"vodId": searchVideoModel.records[i].vodId, "typeId1": typeId1});
                              },
                              child: Container(
                                width: ScreenUtil().getWidthPx(160),
                                height: ScreenUtil().getWidthPx(80),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(20)),
                                  gradient:new LinearGradient(//背景渐变颜色，image 属性为 null 才有用
                                    begin: const Alignment(0.0, -1.0),
                                    end: const Alignment(0.0, 0.6),
                                    colors: <Color>[
                                      const Color(0xff08a0fe),
                                      const Color(0xff5bc4f4)
                                    ],
                                  ),
                                ),
                                child: Cmtext('立即播放', style: TextStyle(fontSize: 12, color: Colors.white),),
                              ),
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      );
      _list.add(_addWidget);
    }
    return _list;
  }

  Widget typeText(typeId1, typeId){
    if(typeId1 == "1") {
      return Cmtext('电影', style: TextStyle(color: Colors.red, fontSize: 13),);
    } else if(typeId1 == "2") {
      return Cmtext('电视剧', style: TextStyle(color: Colors.red, fontSize: 13),);
    } else if(typeId1 == "0") {
      if (typeId == "3") {
        return Cmtext('综艺', style: TextStyle(color: Colors.red, fontSize: 13),);
      } else {
        return Cmtext('动漫', style: TextStyle(color: Colors.red, fontSize: 13),);
      }
    }
  }

  searchFn() async {
    Map<String, dynamic> postData = {
      "vodName": searchText,
      "pageNo": 1,
	    "pageSize": 20
    };
    var _res = await HttpUtil().request("searchVideo", data: postData, need_login: false);
    LogUtil.e(_res);
    if(_res != null && _res['code'] == 200) {
      if(mounted) {
        SearchVideoModel _list = SearchVideoModel.fromJson(_res['data']);
        if (_list.records.length > 0) {
          for (var i = 0; i < _list.records.length; i++) {
            var vodPic = _list.records[i].vodPic;
            if (vodPic != "" && vodPic.substring(0, 6) == "upload") {
              _list.records[i].vodPic = "http://app.ydecai.cn/" + _list.records[i].vodPic;
            }
          }
          setState(() {
            searchVideoModel = _list;
          });
        }
      }
    }
    return _res;
  }
}